﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for WorkSpaceSelectView.xaml
    /// </summary>
    public partial class WorkSpaceSelectView : Window
    {
        private LocalWorkSpace _LocalWorkSpace;
        public WorkSpaceSelectView()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Register(this);
            
            NCLEventBus.Publish(new BeginGetWorkspaceListEvent(UIContext.Current.Token, UIContext.Current.ComputerName));
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NCLEventBus.Unregister(this);
        }

        /// <summary>
        /// 进入工作空间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnWS_Click(object sender, RoutedEventArgs e)
        {
            if (!((sender as Button).Tag as WorkSpaceViewModel).IsMapping)
            {
                MessageBox.Show("该工作空间不存在");
                return;
            }
            SelectWorkSpace((Designtime.Metadata.LocalWorkSpace)((sender as Button).Tag as WorkSpaceViewModel));
        }
        /// <summary>
        /// 新建工作空间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _btnNewWorkSpace_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderBD = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (folderBD.SelectedPath != string.Empty)
                {
                    _LocalWorkSpace = new LocalWorkSpace();
                    _LocalWorkSpace.Computer = UIContext.Current.ComputerName;
                    _LocalWorkSpace.LocalPath = folderBD.SelectedPath + "\\";

                    BeginCreateWorkspaceEvent @event = new BeginCreateWorkspaceEvent(UIContext.Current.Token, _LocalWorkSpace.Computer, _LocalWorkSpace.LocalPath);
                    NCLEventBus.Publish(@event);
                }
            }
        }
        /// <summary>
        /// 选择工作空间
        /// </summary>
        /// <param name="localWorkSpace"></param>
        private void SelectWorkSpace(LocalWorkSpace localWorkSpace)
        {
            LocalStorage.SaveLocalWorkSpace(localWorkSpace);
            UIContext.Current.LocalWorkSpace = localWorkSpace;
            this.Dispatcher.Invoke(() =>
            {
                this.DialogResult = true;
            });
        }
        /// <summary>
        /// 重新获取工作空间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRetry_Click(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Publish(new BeginGetWorkspaceListEvent(UIContext.Current.Token, UIContext.Current.ComputerName));
            _btnRetry.Visibility = Visibility.Collapsed;
            _ctlTbMessage.Visibility = System.Windows.Visibility.Visible;
            _ctlwpListView.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
